package zucc.edu.cn.chengchengparttime.Home;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatButton;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import zucc.edu.cn.chengchengparttime.MainActivity;
import zucc.edu.cn.chengchengparttime.My.MyAppSetting;
import zucc.edu.cn.chengchengparttime.My.MyAppSetting_AccountSecurity;
import zucc.edu.cn.chengchengparttime.R;
import zucc.edu.cn.chengchengparttime.Text;
import zucc.edu.cn.chengchengparttime.Work;
import zucc.edu.cn.chengchengparttime.Workdetails.WorkdetailsActivity;

public class HomeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int HEADER_VIEW=0; //头部
    private static final int NORMAL_VIEW=1; //兼职列表

    private static final String DEBUG_TAG="Chengcheng Log2";
    private Context context;
    private List<Work> dataList;
    View view;
    SQLiteDatabase mDatabase;
    private List<Work> dataList2;
    private HomeAdapter homeAdapter; //适配器
    private RecyclerView rv; //RecyclerView布局

    public HomeAdapter(List<Work> dataList, Context context, View view, SQLiteDatabase mDatabase){
        this.context=context;
        this.dataList=dataList;
        this.view=view;
        this.mDatabase=mDatabase;
    }

    //头部ViewHolder
    public class HeaderViewHolder extends RecyclerView.ViewHolder {
        public HeaderViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(v -> {
                type=(Spinner) v.findViewById(R.id.selecttypes);
                Log.e("click", type.getSelectedItem().toString());
                Intent intent=new Intent(type.getSelectedItem().toString());
                LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
            });
        }
        //数据封装
        public Spinner type;
        //getter和setter方法
        public Spinner getType() {
            return type;
        }

        public void setType(Spinner type) {
            this.type = type;
        }
    }

    //兼职列表ViewHolder
    public class NormalViewHolder extends RecyclerView.ViewHolder {
        public NormalViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(v -> {

            });
        }
        //数据封装
        public TextView workname; //工作名称
        public TextView workarea; //工作地点
        public TextView worktime; //工作时间
        public TextView salaryperhour; //时薪
        public TextView paymentway; //结付方式
        public AppCompatButton btn_signup; //我要报名按钮
        //getter和setter方法
        public TextView getWorkname() {
            return workname;
        }

        public void setWorkname(TextView workname) {
            this.workname = workname;
        }

        public TextView getWorkarea() {
            return workarea;
        }

        public void setWorkarea(TextView workarea) {
            this.workarea = workarea;
        }

        public TextView getWorktime() {
            return worktime;
        }

        public void setWorktime(TextView worktime) {
            this.worktime = worktime;
        }

        public TextView getSalaryperhour() {
            return salaryperhour;
        }

        public void setSalaryperhour(TextView salaryperhour) {
            this.salaryperhour = salaryperhour;
        }

        public TextView getPaymentway() {
            return paymentway;
        }

        public void setPaymentway(TextView paymentway) {
            this.paymentway = paymentway;
        }

        public AppCompatButton getBtn_signup() {
            return btn_signup;
        }

        public void setBtn_signup(AppCompatButton btn_signup) {
            this.btn_signup = btn_signup;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if(viewType==HEADER_VIEW){ //头部
            View headerView= LayoutInflater.from(context).inflate(R.layout.activity_home,null); //定义view
            HeaderViewHolder Headerholder=new HeaderViewHolder(headerView); //定义ViewHolder
            //接下来是控件的引用声明
            Headerholder.type=(Spinner) headerView.findViewById(R.id.selecttypes);

            return Headerholder; //返回holder
        }else{ //兼职列表
            View normalView= LayoutInflater.from(context).inflate(R.layout.activity_home_item,null); //定义view
            NormalViewHolder Normalholder=new NormalViewHolder(normalView); //定义ViewHolder
            //接下来是控件的引用声明
            Normalholder.workname=(TextView) normalView.findViewById(R.id.workname);
            Normalholder.workarea=(TextView) normalView.findViewById(R.id.workarea);
            Normalholder.worktime=(TextView) normalView.findViewById(R.id.worktime);
            Normalholder.salaryperhour=(TextView) normalView.findViewById(R.id.salaryperhour);
            Normalholder.paymentway=(TextView) normalView.findViewById(R.id.paymentway);
            Normalholder.btn_signup=(AppCompatButton) normalView.findViewById(R.id.btn_signup);
            return Normalholder; //返回holder
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        try{
            if(holder instanceof HeaderViewHolder){ //对于头部ViewHolder
                HeaderViewHolder headerViewHolder=(HeaderViewHolder) holder;
                String types = headerViewHolder.type.getSelectedItem().toString();

            }else if(holder instanceof HomeAdapter.NormalViewHolder){ //对于兼职列表ViewHolder
                HomeAdapter.NormalViewHolder normalViewHolder=(HomeAdapter.NormalViewHolder) holder;
                Work data=dataList.get(position-1);
                Log.d("data",data.toString());//测试

                //填充工作名称
                normalViewHolder.workname.setText(data.getWorkName());
                Log.d("workname",normalViewHolder.workname.getText().toString());
                //填充工作地点
                normalViewHolder.workarea.setText(data.getWorkArea());
                Log.d("workarea",normalViewHolder.workarea.getText().toString());
                //填充工作时间
                normalViewHolder.worktime.setText(data.getWorkTime());
                Log.d("worktime",normalViewHolder.worktime.getText().toString());
                //填充时薪
                normalViewHolder.salaryperhour.setText(data.getSalaryPerHour()+"/小时");
                Log.d("salaryperhour",normalViewHolder.salaryperhour.getText().toString());
                //填充结付方式
                normalViewHolder.paymentway.setText(data.getPaymentWay());
                Log.d("paymentway",normalViewHolder.paymentway.getText().toString());
                //报名按钮
                normalViewHolder.btn_signup.setOnClickListener(view -> {
                    Toast.makeText(this.context, "点击了我要报名按钮" , Toast.LENGTH_SHORT).show();

                    Cursor c=mDatabase.query("tbl_publisher", null, null, null, null, null,null);
                    c.moveToFirst();//游标移到第一条记录

                    // 打印记录
                    String[] x = new String[30];
                    while(!c.isAfterLast()){
                        String rowResults = "||";
                        for (int i = 0; i < c.getColumnCount(); i++) {
                            rowResults = rowResults.concat(c.getString(i) + "||");
                            x[i]=c.getString(i);
                        }
                        c.moveToNext();
                    }
                    c.close();//关闭游标工厂

                    //跳转到兼职具体信息界面
                    Intent editpro=new Intent(context,WorkdetailsActivity.class);
                    editpro.putExtra("item",data.getWorkID()); //传递当前选中item的id
                    context.startActivity(editpro);
                });
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    //重写getItemCount方法
    @Override
    public int getItemCount() {
        return dataList.size()+1;
    }

    //重写getItemViewType方法
    @Override
    public int getItemViewType(int position){
        if(position==0){ //第一个item就是背景
            return HEADER_VIEW;
        }else{ //其余是每条兼职信息
            return NORMAL_VIEW;
        }
    }
}
